/**
 * @author tibi
 */
Ext.namespace("Softab");

Softab.CurrentEntityID = 0;
Softab.EntityAddRecordHandler = function(entityID){
    Softab.CurrentEntityID = entityID;
    Ext.Ajax.request({
        url: 'server/EntityAddRecordForm.php?__id=' + entityID + '&task=EntityDetails',
        success: function(response){
            Softab.EntityAddRecordHandler.ParseEntityDetails(response)
        }
    });
}


Ext.namespace("Softab.EntityAddRecordHandler");
Softab.EntityAddRecordHandler.EntityDetails = {
    id: "0",
    name: "Not Set"
}
Softab.EntityAddRecordHandler.ParseEntityDetails = function(response){
    entityRes = eval(response.responseText);
    status = eval(entityRes.Success);
    if (status) {
        entity = eval(entityRes.Entity);
        Softab.EntityAddRecordHandler.EntityDetails.id = entity[0].ID;
        Softab.EntityAddRecordHandler.EntityDetails.name = entity[0].Label;
        extFields = eval(entityRes.ExtFields);
        customFields = eval(entityRes.CustomClientFields);
        Softab.EntityAddRecordWin(extFields, customFields, entity[0].ID)
    }
}
//Ext.namespace("Softab");
Softab.EntityAddRecordWin = function(extFields, customFields){
    var win = new Ext.Window({
        width: 720,
        id: 'autoload-win',
        autoScroll: true,
        modal: true,
        layout: 'fit',
        height: 600,
        onEsc: function(){
            this.close()
        },
        title: 'Adaugare ' + Softab.EntityAddRecordHandler.EntityDetails.name,
        listeners: {
            show: function(){
                this.loadMask = new Ext.LoadMask(this.body, {
                    msg: 'Loading. Please wait...'
                });
            }
        },
        tbar: [new Ext.Button({
			id:"AddRecordBtn",
            text: 'Save',
			disabled:true,
			handler: function(){
                var formObj = Ext.getCmp('myForm');
                if (formObj.getForm().isValid()) {
                    formObj.getForm().submit({
                        url: 'server/EntityAddRecordForm.php?task=save',
                        waitMsg: 'Saving record...',
                        success: function(entityForm, ret){
                            //msg('Success', 'Processed file "'+o.result.file+'" on the server');
                            //msg('Success', 'Form saved with success');
							var toReload = Ext.getCmp(Softab.EntityAddRecordHandler.EntityDetails.id + "_EditorGrid");
							toReload.store.load({
		                        params: {
		                            start: 0,
		                            limit: 15
		                        }
		                    });
                            Ext.Msg.show({
                                title: "Success",
                                msg: ret.result.message,
                                minWidth: 200,
                                modal: true,
                                icon: Ext.Msg.INFO,
                                buttons: Ext.Msg.OK
                            });
							win.close();
                        },
						failure: function(entityForm, ret){
							//msg('Success', 'Processed file "'+o.result.file+'" on the server');
							//msg('Success', 'Form saved with success');
							Ext.Msg.show({
								title: "Failure",
								msg: ret.result.message,
								minWidth: 200,
								modal: true,
								icon: Ext.Msg.INFO,
								buttons: Ext.Msg.OK
							});
						}
                    });
                }
                
            }
        }), new Ext.Button({
            text: 'Cancel',
            handler: function(){
                win.close();
            }
        })]
    });
    fields = new Array();
    for (i = 0; i < extFields.length; i++) {
        field = new Softab.EntityField();
        done = field._init(extFields[i], customFields[i]);
        fields.push(done);
    }
    var entityForm = new Ext.FormPanel({
        id: 'myForm',
        fileUpload: true,
        standardSubmit: false,
		bodyBorder: false,
		headerAsText: false,
		header: false,
        frame: true,
        autoScroll: true,
        paddings: '5px 5px 5px 5px',
        labelWidth: 150,
        defaultType: "textfield",
        buttonAlign: "center",
		monitorValid: true,
        defaults: {
            anchor: '95%'
        },
        items: fields,
		/*
        buttons: [new Ext.Button({
            text: 'Salveaza',
            handler: function(){
                if (entityForm.getForm().isValid()) {
                    entityForm.getForm().submit({
                        url: 'server/EntityAddRecordForm.php?task=save',
                        waitMsg: 'Uploading your photo...',
                        success: function(entityForm, o){
                            //msg('Success', 'Processed file "'+o.result.file+'" on the server');
                            //msg('Success', 'Form saved with success');
                            Ext.Msg.show({
                                title: "Success",
                                msg: 'Form saved with success',
                                minWidth: 200,
                                modal: true,
                                icon: Ext.Msg.INFO,
                                buttons: Ext.Msg.OK
                            });
                            
                        }
                    });
                }
            }
        })]*/
    });
	//Ext.get("AddRecordBtn").disabled = false;
	var tmp;
    entityForm.on("clientvalidation", function(form, state){
			if(state){
				Ext.getCmp("AddRecordBtn").enable();
			}else{
				Ext.getCmp("AddRecordBtn").disable();
			}
		});
    win.add(entityForm);
    win.show();
    /*OldWayFormBuilder 
     Ext.Ajax.request({
     url: 'server/EntityAddRecordForm.php?__id=' + Softab.EntityAddRecordHandler.EntityDetails.id + '&add',
     success: function(response){
     r = response.responseText;
     formItems = eval(r);
     var entityForm = new Ext.FormPanel({
     id: 'myForm',
     standardSubmit: false,
     title: 'TitluPanel',
     action: 'server/EntitySave.php',
     width: 'auto',
     autoScroll: true,
     labelWidth: 150,
     height: 570,
     isUpload: true,
     defaultType: "textfield",
     items: fields,
     buttonAlign: "center",
     buttons: [new Ext.Button({
     text: 'Salveaza',
     handler: function(){
     entityForm.getForm().doAction('submit', {
     url: 'server/EntitySave.php'
     })
     
     //														entityForm.getForm().Action.Submit({
     //															url:'server/EntitySave.php'
     //															});
     //														entityForm.getForm().getEl().dom.action = 'server/EntitySave.php';
     //													    entityForm.getForm().getEl().dom.method = 'POST';
     //														entityForm.getForm().getEl().dom.enctype = 'multipart/form-data';
     //														entityForm.getForm().getEl().dom.target = 'hiddenIframe';
     //												        entityForm.getForm().submit();
     }
     
     })]
     });
     
     win.add(entityForm);
     win.show();
     
     }
     });*/
}
